package Schema::Result::Aggregate;

use strict;
use warnings;
use utf8;

use base qw/DBIx::Class::Core/;

__PACKAGE__->table('Aggregate');
__PACKAGE__->add_columns( 
    Id => { data_type => 'integer', is_auto_increment => 1 }, 
    ProjectId  => {},
    Name  => {},
    Content => {},
    Comment  => {},
);
__PACKAGE__->set_primary_key('Id');
__PACKAGE__->belongs_to(Project => 'Schema::Result::Project', 'ProjectId');
__PACKAGE__->has_many(Models => 'Schema::Result::Model', 'AggregateId', { cascade_delete => 1 });

# 创建模型
sub Create {
    my ($class, $schema,  $param) = @_;

    my $aggregateVo = $param->{AggregateVo};

    # 创建新的聚合
    my $newAggregate = $schema->resultset('Aggregate')->create(
        {
            ProjectId => $param->{ProjectId},
            Name => $aggregateVo->{Name},
            Content => $aggregateVo->{Content},
            Comment => "Empty Now",
        }
    );

    # 创建聚合中的模型
    foreach my $modelVo (@{$aggregateVo->{ModelVos}}){
        my $newModel = Schema::Result::Model->Create(
            $schema,
            {
               AggregateId => $newAggregate->Id,
               ModelIdentityId => $modelVo->{IdentityId},
               ModelVo => $modelVo,
            }
        );
    }
}

# 删除
sub Delete {
    my ($self) = @_;

    $self->delete;
}

1;
